{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "392f82b1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.6a06396b65d1791b8dbfe3e28bb8a97d.2592000.1652270968.282335-25946217'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?'\n",
    "payload = {\n",
    "    'grant_type':'client_credentials',\n",
    "    'client_id':'kY2nTBbrpGrkwqMVoxHy3Rph',\n",
    "    'client_secret':'pxI43pbMKZEWgVl0THGj0nVq8bAxfXvW'\n",
    "}\n",
    "response = requests.get(host,params=payload)\n",
    "token = response.json()['access_token']\n",
    "token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "d2130dd7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 封装函数\n",
    "import requests\n",
    "import base64\n",
    "def everything(token,image_path,url):\n",
    "    request_url = url\n",
    "    # 二进制方式打开图片文件\n",
    "    f = open(image_path, 'rb')\n",
    "    img = base64.b64encode(f.read())\n",
    "    \n",
    "    params = {\n",
    "        \"image\":img,\n",
    "        \"id_card_side\":\"front\"\n",
    "             }\n",
    "    access_token = token\n",
    "    request_url = request_url + \"?access_token=\" + access_token\n",
    "    headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "    response = requests.post(request_url, data=params, headers=headers)\n",
    "    return response.json()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "e1b3fdf1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'words_result': [{'words': '魃魈魁鬾'},\n",
       "  {'words': '魑魅魍魉'},\n",
       "  {'words': '又双叒叕'},\n",
       "  {'words': '水冰淼㵘'},\n",
       "  {'words': '火炏焱燚'}],\n",
       " 'words_result_num': 5,\n",
       " 'log_id': 1513493732777735354}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通用文字识别\n",
    "url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic\"\n",
    "everything(token,\"image.jpg\",url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "17827791",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'words_result': [{'words': '抱朕'}, {'words': '还等朕翻你牌子呐?'}],\n",
       " 'words_result_num': 2,\n",
       " 'log_id': 1513495291175946048}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 网络图片文字识别\n",
    "url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/webimage\"\n",
    "everything(token,\"image2.png\",url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "b03410b1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'codes_result': [{'text': ['vegetabledog'], 'type': 'QR_CODE'}],\n",
       " 'codes_result_num': 1,\n",
       " 'log_id': 1513490479404165154}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 二维码识别\n",
    "url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/qrcode\"\n",
    "everything(token,\"code.png\",url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "9ff846ce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'words_result_num': 43,\n",
       " 'words_result': {'AmountInWords': '捌佰捌拾贰万陆仟肆佰圆整',\n",
       "  'InvoiceNumConfirm': '00954188',\n",
       "  'CommodityEndDate': [],\n",
       "  'CommodityVehicleType': [],\n",
       "  'CommodityStartDate': [],\n",
       "  'CommodityPrice': [{'row': '1', 'word': '680'}],\n",
       "  'NoteDrawer': '徐俊云',\n",
       "  'SellerAddress': '山西长治市长子东旺7827148',\n",
       "  'CommodityNum': [{'row': '1', 'word': '12980'}],\n",
       "  'SellerRegisterNum': '140400538915790',\n",
       "  'MachineCode': '',\n",
       "  'Remarks': '',\n",
       "  'SellerBank': '中国农业银行长治市城区支行04-716001040126365',\n",
       "  'CommodityTaxRate': [{'row': '1', 'word': '17%'}],\n",
       "  'TotalTax': '1282468.38',\n",
       "  'InvoiceCodeConfirm': '1400102620',\n",
       "  'CheckCode': '3218321804639921980',\n",
       "  'InvoiceCode': '1400102620',\n",
       "  'InvoiceDate': '2010年12月20日',\n",
       "  'PurchaserRegisterNum': '110111102757725',\n",
       "  'InvoiceTypeOrg': '山西增值税普通发票',\n",
       "  'OnlinePay': '',\n",
       "  'Password': '<3008-7-21-32>0><4*+<-4<+6>0466/35>4+170<5-*39>32-47205>9*3777*5<8>++76/46038*>/<5<>',\n",
       "  'Agent': '否',\n",
       "  'AmountInFiguers': '8826400.00',\n",
       "  'PurchaserBank': '中国工商银行北京新中街支行0200242419020140523',\n",
       "  'Checker': '刘建',\n",
       "  'City': '',\n",
       "  'TotalAmount': '7543931.62',\n",
       "  'CommodityAmount': [{'row': '1', 'word': '7543931.62'}],\n",
       "  'PurchaserName': '北京亚能燃料有限公司',\n",
       "  'CommodityType': [{'row': '1', 'word': '发热量20.908MJ/KG'}],\n",
       "  'Province': '山西',\n",
       "  'InvoiceType': '普通发票',\n",
       "  'SheetNum': '第二联：发票联',\n",
       "  'PurchaserAddress': '北京市房山区大石河010-89340523',\n",
       "  'CommodityTax': [{'row': '1', 'word': '1282468.38'}],\n",
       "  'CommodityPlateNum': [],\n",
       "  'CommodityUnit': [{'row': '1', 'word': '吨'}],\n",
       "  'Payee': '陆梁盛',\n",
       "  'CommodityName': [{'row': '1', 'word': '混煤'}],\n",
       "  'SellerName': '山西华晟荣煤矿有限公司',\n",
       "  'InvoiceNum': '00954188'},\n",
       " 'log_id': 1513491913180543618}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 增值税发票识别\n",
    "url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/vat_invoice\"\n",
    "everything(token,\"fapiao.jpg\",url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "75216b1a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'words_result': {'姓名': {'location': {'top': 39,\n",
       "    'left': 96,\n",
       "    'width': 67,\n",
       "    'height': 25},\n",
       "   'words': '韦小宝'},\n",
       "  '民族': {'location': {'top': 82, 'left': 197, 'width': 17, 'height': 20},\n",
       "   'words': '汉'},\n",
       "  '住址': {'location': {'top': 159, 'left': 90, 'width': 219, 'height': 55},\n",
       "   'words': '北京市东城区景山前街4号紫禁城敬事房'},\n",
       "  '公民身份号码': {'location': {'top': 0, 'left': 0, 'width': 0, 'height': 0},\n",
       "   'words': ''},\n",
       "  '出生': {'location': {'top': 0, 'left': 0, 'width': 0, 'height': 0},\n",
       "   'words': ''},\n",
       "  '性别': {'location': {'top': 81, 'left': 95, 'width': 19, 'height': 22},\n",
       "   'words': '男'}},\n",
       " 'words_result_num': 6,\n",
       " 'idcard_number_type': 0,\n",
       " 'image_status': 'unknown',\n",
       " 'log_id': 1513492349926438860}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 身份证识别\n",
    "url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/idcard\"\n",
    "everything(token,\"idcard.jpg\",url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cbeb9f2e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
